//
//  WBImagePickVC.m
//  UILbrary
//
//  Created by Apple on 2023/3/3.
//  Copyright © 2023 mac. All rights reserved.
//

#import "WBImagePickVC.h"
#import "BBImagePickHandle.h"

@interface WBImagePickVC ()<BBImagePickHandleDelegate>
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation WBImagePickVC



//MARK: - system


- (instancetype)init
{
    self = [super init];
    if(self){
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self p_configSubviews];
    [self test];
}


//MARK: - test

- (void)test
{
    self.imageView.backgroundColor = [UIColor redColor];
    self.button.backgroundColor = [UIColor systemBlueColor];
}

//MARK: - BBImagePickHandleDelegate

- (void)picker:(BBImagePickHandle *)picker didFinishPicking:(UIImage *)image
{
    NSLog(@"didFinishPicking befor data size = %ld",UIImageJPEGRepresentation(image, 1.0).length);
    
    NSData *imgData = [NSObject compressImageToData:image targetSize:1024 * 1024 * 0.01];
    
    NSLog(@"didFinishPicking after data size = %ld",imgData.length);

    
    UIImage *compressImage = [UIImage imageWithData:imgData];
    self.imageView.image = compressImage;
}

//MARK: - private

- (void)p_configSubviews
{
    [self.view addSubview:self.button];
    [self.view addSubview:self.imageView];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).offset(40);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.button.mas_bottom).offset(10);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
    }];
}

//MARK: - action

- (void)buttonClickAction
{
    BBImagePickHandle *pick = [BBImagePickHandle new];
    pick.vc = self;
    pick.delegate = self;
    
    [pick presentPickViewContoller];
    }

//MARK: - get


- (UIImageView *)imageView
{
    if(!_imageView){
        _imageView = [UIImageView new];
    }
    return _imageView;
}

- (UIButton *)button
{
    if(!_button){
        _button = [UIButton new];
        [_button setTitle:@"选择照片" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(buttonClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

@end
